package com.easy.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class Chinese {
	public static String getSpelling(String src)
	{
		char[] t1 = null;
		t1 = src.toCharArray(); 
		// System.out.println(t1.length); 
		String[] t2 = new String[t1.length]; 
		// System.out.println(t2.length); 
		// 设置汉字拼音输出的格式 
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE); 
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
		t3.setVCharType(HanyuPinyinVCharType.WITH_V); 
		String t4 = ""; 
		String tmpt="";
		int t0 = t1.length; 
		try { 
			for (int i = 0; i < t0; i++) { 
				// 判断是否为汉字字符 
				// System.out.println(t1[i]); 
				if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){ 
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
					tmpt=t2[0];
					if(tmpt.length()>1)tmpt=t2[0].charAt(0)+(t2[0].substring(1).toLowerCase());
					t4 += tmpt+" ";// 取出该汉字全拼的第一种读音并连接到字符串t4后 
				}
				else { 
				// 如果不是汉字字符，直接取出字符并连接到字符串t4后 
					t4 += Character.toString(t1[i]); 
				} 
			}
		} 
		catch (BadHanyuPinyinOutputFormatCombination e) { 
		// TODO Auto-generated catch block 
			e.printStackTrace(); 
		}
		if(t4.length()>0){
			if(t4.charAt(t4.length()-1)==' ')t4=t4.substring(0,t4.length()-1);
		}
		return t4; 
	}        
	public static String getFistPY(String src)
	{
		String pycode=src;
		String convert = "";  
		for (int j = 0; j < pycode.length(); j++) {  
			char word = pycode.charAt(j);  
			if((word>='0' && word<='9') || (word>='a' && word<='z')  || (word>='A' && word<='Z') ){
				convert += word;
				continue;
			}
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
			if (pinyinArray != null) {  
				convert += pinyinArray[0].charAt(0);  
			}else {  
				//convert += word;  
			}  
		}  
		return convert;
	}
	public static String getFullPY(String src)
	{
		char[] t1 = null;
		t1 = src.toCharArray(); 
		// System.out.println(t1.length); 
		String[] t2 = new String[t1.length]; 
		// System.out.println(t2.length); 
		// 设置汉字拼音输出的格式 
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); 
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE); 
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
		t3.setVCharType(HanyuPinyinVCharType.WITH_V); 
		String t4 = ""; 
		String tmpt="";
		int t0 = t1.length; 
		try { 
			for (int i = 0; i < t0; i++) { 
				// 判断是否为汉字字符 
				// System.out.println(t1[i]); 
				if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){ 
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
					tmpt=t2[0];
					if(tmpt.length()>1)tmpt=t2[0].charAt(0)+(t2[0].substring(1).toLowerCase());
					t4 += tmpt;// 取出该汉字全拼的第一种读音并连接到字符串t4后 
				}
				else { 
				// 如果不是汉字字符，直接取出字符并连接到字符串t4后 
					t4 += Character.toString(t1[i]); 
				} 
			}
		} 
		catch (BadHanyuPinyinOutputFormatCombination e) { 
		// TODO Auto-generated catch block 
			e.printStackTrace(); 
		}
		return t4; 
	}
	public static String[] getSurname(String name)
	{
		String[] hyphenated = new String[]{"欧阳","太史","端木","上官","司马","东方","独孤","南宫","万俟","闻人","夏侯","诸葛","尉迟","公羊","赫连","澹台","皇甫",  
		                     "宗政","濮阳","公冶","太叔","申屠","公孙","慕容","仲孙","钟离","长孙","宇文","城池","司徒","鲜于","司空","汝嫣","闾丘","子车","亓官",  
		                     "司寇","巫马","公西","颛孙","壤驷","公良","漆雕","乐正","宰父","谷梁","拓跋","夹谷","轩辕","令狐","段干","百里","呼延","东郭","南门",  
		                     "羊舌","微生","公户","公玉","公仪","梁丘","公仲","公上","公门","公山","公坚","左丘","公伯","西门","公祖","第五","公乘","贯丘","公皙",  
		                     "南荣","东里","东宫","仲长","子书","子桑","即墨","达奚","褚师"};
		String []surname=new String[2];
		int rc=0;
		char[] t1 = name.toCharArray(); 
		for (int i = 0; i < t1.length; i++) { 
			if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")){
				rc=1;
				break;
			}
		}
		if(rc==0){
			int pos=name.indexOf(" ");
			if(pos<1){
				surname[0]=name;
				surname[1]="";
			}
			else {
				surname[0]=name.substring(0,pos);
				surname[1]=name.substring(pos+1);
			}
			return surname;
		}

		for(int i=0;i<hyphenated.length;i++){
			if(name.indexOf(hyphenated[i])==0){
				surname[0]=hyphenated[i];
				surname[1]=name.replaceFirst(hyphenated[i], "");
				return surname;
			}
		}
		surname[0]=name.substring(0,1);
		surname[1]=name.substring(1);
		return surname;  
	}
}
